package com.wx.servlet;

import com.wx.entry.SmbmsUser;
import com.wx.service.LoginService;
import com.wx.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String userCode = req.getParameter("userCode");
            String userPassword = req.getParameter("userPassword");
            //servlet 层，职责  1.负责页面数据传递，2.调用service 层实现业务逻辑  3.页面跳转。
            //正常创建
            //LoginServiceImpl  loginService  = new LoginServiceImpl();
            //多态和接口
             LoginService  loginService = new LoginServiceImpl();
             boolean result =  loginService.login(userCode,userPassword);
             if(result){
                 HttpSession httpSession = req.getSession();
                 httpSession.setAttribute("userName",userCode);
                 resp.sendRedirect("jsp/frame.jsp");
             }else {
                 HttpSession httpSession = req.getSession();
                 httpSession.setAttribute("error","用户名或密码错误");
                 resp.sendRedirect("login.jsp");
             }
    }
}
